<html>
	<head>
		<title>WaveDrom Tools</title>
		<script type="text/javascript" src="skins/default.js"></script>
		<script type="text/javascript" src="WaveDrom.js"></script>
	</head>
	<body>
		<h1>WaveDrom Tools</h1>
		<div id='SVG'><div id="WaveDrom_Display_0"></div></div>
		<div id='TXT'><textarea id="InputJSON_0"></textarea></div>
		<script>

	var gui, win, fnames;
	gui = require('nw.gui');
	mfs = require('fs');
	win = gui.Window.get();

	function owrite (err) {
		if (err) {
			win.show();
			window.setTimeout(function () {gui.App.quit();}, 2000);
			throw err;
		}
		gui.App.quit();
	}
	function iread (err, data) {
		var txt, svg, ser;
		if (err) {
			win.show();
			window.setTimeout(function () {gui.App.quit();}, 2000);
			throw err;
		}
		txt = document.getElementById ('InputJSON_0');
		txt.value = data;
		WaveDrom.EditorRefresh();
		svg = document.getElementById ('svgcontent_0');
		ser = new XMLSerializer();
		mfs.writeFile(fnames[1], ser.serializeToString(svg), owrite)
	}

	fnames = gui.App.argv;
	if (fnames.length !== 2) {
		document.writeln('expected 2 file names, got ' + fnames.length + '. bye.<br>');
		win.show();
		window.setTimeout(function () {gui.App.quit();}, 2000);
	} else {
		mfs.readFile(fnames[0], iread);
	}
		</script>
	</body>
</html>
